﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Jayden.Dll.Core;
using Jayden.Dll.Core.Persistence;
using Jayden.Dll.Cards.Poker;
using Jayden.Dll.Cards.Poker.Enums;

namespace Jayden.Windows.PokerWatcher
{
	public partial class PokerWatcherTableViewerForm : Form
	{
		public Configuration Configuration { get; set; }
		public UnitOfWork UnitOfWork { get; set; }
		private TexasHoldemHandSummary Summary { get; set; }


		public PokerWatcherTableViewerForm(Configuration config, UnitOfWork uow, TexasHoldemHandSummary summary)
		{
			Configuration = config;
			UnitOfWork = uow;
			Summary = summary;

			InitializeComponent();

			TexasHoldemPlayerHandSummaryDataAccess playerHandDataAccess = UnitOfWork.DataAccessManager.GetDataAccess<TexasHoldemPlayerHandSummary>() as TexasHoldemPlayerHandSummaryDataAccess;
			List<TexasHoldemPlayerHandSummary> players = playerHandDataAccess.ReadByHandId(summary.TexasHoldemHandSummaryId);

			foreach (TexasHoldemPlayerHandSummary player in players)
			{
				string[] values = new string[]
				{
					player.PlayerName,
					(player.PlayerStack / 100.0).ToString("0.00") + " " + summary.Currency,
					player.PlayerCards == null ? string.Empty : player.PlayerCards.ToString(),
					(player.PlayerBalance / 100.0).ToString("0.00") + " " + summary.Currency,
					player.Position.ToString()
				};

				ListViewItem item = new ListViewItem(values);
				lvSeats.Items.Add(item);
			}

			TexasHoldemHandActionDataAccess actionDataAccess = UnitOfWork.DataAccessManager.GetDataAccess<TexasHoldemHandAction>() as TexasHoldemHandActionDataAccess;
			List<TexasHoldemHandAction> actions = actionDataAccess.ReadByHandId(summary.TexasHoldemHandSummaryId);

			PokerStreetType previousStreet = PokerStreetType.PreFlop;
			foreach (TexasHoldemHandAction action in actions)
			{
				if (action.Street != previousStreet)
				{
					ListViewItem community = null;
					if (action.Street == PokerStreetType.Flop)
						community = new ListViewItem(new string[] { string.Empty, string.Empty, string.Empty, summary.Flop });
					else if (action.Street == PokerStreetType.Turn)
						community = new ListViewItem(new string[] { string.Empty, string.Empty, string.Empty, summary.Turn });
					else if(action.Street == PokerStreetType.River)
						community = new ListViewItem(new string[] { string.Empty, string.Empty, string.Empty, summary.River });
					if (community != null)
						lvActions.Items.Add(community);
					previousStreet = action.Street;
				}
				string[] values = new string[]
				{
					action.PlayerName,
					action.Amount == 0 ? string.Empty : ((action.Amount / 100.0).ToString("0.00") + " " + summary.Currency),
					action.ActionType.ToString()
				};
				ListViewItem item = new ListViewItem(values);
				lvActions.Items.Add(item);
			}
			if(previousStreet <= PokerStreetType.PreFlop)
				lvActions.Items.Add(new ListViewItem(new string[] { string.Empty, string.Empty, string.Empty, summary.Flop }));
			if(previousStreet <= PokerStreetType.Flop)
				lvActions.Items.Add(new ListViewItem(new string[] { string.Empty, string.Empty, string.Empty, summary.Turn }));
			if(previousStreet <= PokerStreetType.Turn)
				lvActions.Items.Add(new ListViewItem(new string[] { string.Empty, string.Empty, string.Empty, summary.River }));
		}


	}
}
